


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: ClassLookup</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.dormancy.util</a> ]
</div>

<h1>Coverage Summary for Class: ClassLookup (at.dormancy.util)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">ClassLookup</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (8/ 8)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (25/ 25)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2013 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.dormancy.util;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import org.apache.commons.lang.ArrayUtils;
<i>19</i>&nbsp;import org.apache.commons.lang.StringUtils;
<i>20</i>&nbsp;
<i>21</i>&nbsp;import javax.annotation.Nonnull;
<i>22</i>&nbsp;import javax.annotation.Nullable;
<i>23</i>&nbsp;import java.util.ArrayList;
<i>24</i>&nbsp;import java.util.Collections;
<i>25</i>&nbsp;import java.util.List;
<i>26</i>&nbsp;
<i>27</i>&nbsp;/**
<i>28</i>&nbsp; * Performs class lookups using the default {@link ClassLoader}.
<i>29</i>&nbsp; * &lt;p/&gt;
<i>30</i>&nbsp; * Note that none of the methods throw a {@link ClassNotFoundException}. Instead, {@code null} is returned unless
<i>31</i>&nbsp; * something else is provided.
<i>32</i>&nbsp; *
<i>33</i>&nbsp; * @author Gregor Schauer
<i>34</i>&nbsp; * @since 2.0.0
<i>35</i>&nbsp; */
<i>36</i>&nbsp;public final class ClassLookup {
<b class="fc"><i>37</i>&nbsp;	private List&lt;String&gt; names = new ArrayList&lt;String&gt;(1);</b>
<i>38</i>&nbsp;	private Class&lt;?&gt; defaultClass;
<i>39</i>&nbsp;	private Exception exception;
<i>40</i>&nbsp;
<i>41</i>&nbsp;	/**
<i>42</i>&nbsp;	 * Returns the {@link Class} object associated with the class or interface with the given name.
<i>43</i>&nbsp;	 * &lt;p/&gt;
<i>44</i>&nbsp;	 * If a {@link ClassNotFoundException} was thrown by the {@link ClassLoader} performing the lookup, it gets caught
<i>45</i>&nbsp;	 * gracefully and {@code null} is returned.&lt;br/&gt;
<i>46</i>&nbsp;	 * Any other serious exception such as {@link ExceptionInInitializerError} or {@link LinkageError} is forwarded to
<i>47</i>&nbsp;	 * the caller immediately.
<i>48</i>&nbsp;	 *
<i>49</i>&nbsp;	 * @param name the full-qualified name of the desired class.
<i>50</i>&nbsp;	 * @return the {@code Class} object for the class with the specified name or {@code null}.
<i>51</i>&nbsp;	 * @see Class#forName(String)
<i>52</i>&nbsp;	 */
<i>53</i>&nbsp;	@Nullable
<i>54</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>55</i>&nbsp;	public static &lt;T&gt; Class&lt;T&gt; forName(@Nonnull String name) {
<i>56</i>&nbsp;		try {
<b class="fc"><i>57</i>&nbsp;			return (Class&lt;T&gt;) Class.forName(StringUtils.defaultIfEmpty(name, &quot;&quot;));</b>
<b class="fc"><i>58</i>&nbsp;		} catch (ClassNotFoundException e) {</b>
<b class="fc"><i>59</i>&nbsp;			return null;</b>
<i>60</i>&nbsp;		}
<i>61</i>&nbsp;	}
<i>62</i>&nbsp;
<i>63</i>&nbsp;	/**
<i>64</i>&nbsp;	 * Creates a new {@code ClassLookup} instance.
<i>65</i>&nbsp;	 *
<i>66</i>&nbsp;	 * @param names the full-qualified names of the classes to lookup
<i>67</i>&nbsp;	 * @return the newly created instance
<i>68</i>&nbsp;	 */
<i>69</i>&nbsp;	@Nonnull
<i>70</i>&nbsp;	public static ClassLookup find(@Nullable String... names) {
<b class="fc"><i>71</i>&nbsp;		return new ClassLookup(names);</b>
<i>72</i>&nbsp;	}
<i>73</i>&nbsp;
<b class="fc"><i>74</i>&nbsp;	private ClassLookup(@Nullable String... names) {</b>
<b class="fc"><i>75</i>&nbsp;		Collections.addAll(this.names, ArrayUtils.nullToEmpty(names));</b>
<b class="fc"><i>76</i>&nbsp;	}</b>
<i>77</i>&nbsp;
<i>78</i>&nbsp;	/**
<i>79</i>&nbsp;	 * Searches for a {@link Class} object associated with the class or interface with the given name.
<i>80</i>&nbsp;	 *
<i>81</i>&nbsp;	 * @param name the full-qualified name of the desired class
<i>82</i>&nbsp;	 * @return this instance
<i>83</i>&nbsp;	 */
<i>84</i>&nbsp;	@Nonnull
<i>85</i>&nbsp;	public ClassLookup or(@Nonnull String name) {
<b class="fc"><i>86</i>&nbsp;		this.names.add(name);</b>
<b class="fc"><i>87</i>&nbsp;		return this;</b>
<i>88</i>&nbsp;	}
<i>89</i>&nbsp;
<i>90</i>&nbsp;	/**
<i>91</i>&nbsp;	 * Sets the {@link Class} to return in case of none of the lookups was successful.
<i>92</i>&nbsp;	 *
<i>93</i>&nbsp;	 * @param defaultClass the {@code Class} to return in case every lookup failed
<i>94</i>&nbsp;	 * @return this instance
<i>95</i>&nbsp;	 */
<i>96</i>&nbsp;	@Nonnull
<i>97</i>&nbsp;	public ClassLookup or(@Nullable Class&lt;?&gt; defaultClass) {
<b class="fc"><i>98</i>&nbsp;		this.defaultClass = defaultClass;</b>
<b class="fc"><i>99</i>&nbsp;		return this;</b>
<i>100</i>&nbsp;	}
<i>101</i>&nbsp;
<i>102</i>&nbsp;	/**
<i>103</i>&nbsp;	 * Throws the given {@link Exception} in case of none of the lookups was successful.
<i>104</i>&nbsp;	 *
<i>105</i>&nbsp;	 * @param e the exception to throw
<i>106</i>&nbsp;	 * @return this instance
<i>107</i>&nbsp;	 */
<i>108</i>&nbsp;	@Nonnull
<i>109</i>&nbsp;	public ClassLookup orThrow(@Nullable Exception e) {
<b class="fc"><i>110</i>&nbsp;		this.exception = e;</b>
<b class="fc"><i>111</i>&nbsp;		return this;</b>
<i>112</i>&nbsp;	}
<i>113</i>&nbsp;
<i>114</i>&nbsp;	/**
<i>115</i>&nbsp;	 * Throws a {@link ClassNotFoundException} in case of none of the lookups was successful.
<i>116</i>&nbsp;	 *
<i>117</i>&nbsp;	 * @param msg the detail message
<i>118</i>&nbsp;	 * @return this instance
<i>119</i>&nbsp;	 * @see #orThrow(Exception)
<i>120</i>&nbsp;	 */
<i>121</i>&nbsp;	@Nonnull
<i>122</i>&nbsp;	public ClassLookup orThrow(@Nullable String msg, @Nullable Object... args) {
<b class="fc"><i>123</i>&nbsp;		return orThrow(new ClassNotFoundException(msg != null ? String.format(msg, args) : null));</b>
<i>124</i>&nbsp;	}
<i>125</i>&nbsp;
<i>126</i>&nbsp;	/**
<i>127</i>&nbsp;	 * Performs the lookup with the full-qualified class names until a {@code Class} was found.
<i>128</i>&nbsp;	 * &lt;p/&gt;
<i>129</i>&nbsp;	 * If an exception was provided, it is thrown if no class was found. Otherwise, the provided default class or
<i>130</i>&nbsp;	 * {@code null} is returned if none was set.
<i>131</i>&nbsp;	 * &lt;p/&gt;
<i>132</i>&nbsp;	 * If the exception is checked i.e., it is neither of type {@link RuntimeException} nor {@link Error}, it gets
<i>133</i>&nbsp;	 * wrapped in a {@link RuntimeException}.
<i>134</i>&nbsp;	 *
<i>135</i>&nbsp;	 * @return the class found or {@code null} if the lookup failed and no default was provided
<i>136</i>&nbsp;	 */
<i>137</i>&nbsp;	@Nullable
<i>138</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>139</i>&nbsp;	public &lt;T&gt; Class&lt;T&gt; get() {
<b class="fc"><i>140</i>&nbsp;		for (String name : names) {</b>
<b class="fc"><i>141</i>&nbsp;			Class&lt;T&gt; clazz = forName(name);</b>
<b class="fc"><i>142</i>&nbsp;			if (clazz != null) {</b>
<b class="fc"><i>143</i>&nbsp;				return clazz;</b>
<i>144</i>&nbsp;			}
<b class="fc"><i>145</i>&nbsp;		}</b>
<b class="fc"><i>146</i>&nbsp;		if (exception != null) {</b>
<b class="fc"><i>147</i>&nbsp;			if (exception instanceof RuntimeException) {</b>
<b class="fc"><i>148</i>&nbsp;				throw (RuntimeException) exception;</b>
<i>149</i>&nbsp;			}
<b class="fc"><i>150</i>&nbsp;			throw new RuntimeException(exception);</b>
<i>151</i>&nbsp;		}
<b class="fc"><i>152</i>&nbsp;		return (Class&lt;T&gt;) defaultClass;</b>
<i>153</i>&nbsp;	}
<i>154</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2013-07-31 23:31</div>
</div>
</body>
</html>
